import re
from flask import Blueprint,request,jsonify
import json
from werkzeug.security import generate_password_hash,check_password_hash
from db import db1
from flask_mail import Mail,Message
from flask import current_app as app
from werkzeug.utils import redirect
bp_user=Blueprint('user',__name__)
 

def send_mail(title,email,content):
    mail = Mail(app)
    msg = Message(title,sender='c1061418032@163.com',recipients=[email])
    msg.ttml = content
    mail.send(msg)

@bp_user.route('/reg',methods=['POST'])
def reg():
    data = request.data
    if data:
        data = json.loads(data)
    else:
        data = request.form
    sql= "insert into users(nikename,password,email) values('%s','%s','%s')"%(data['nikename'],generate_password_hash(data['password']),data['email'])
    db1.update(sql)
    send_mail('欢迎使用',data['email'],"欢迎使用，点击以下<a href='http://localhost:5000/user/validEmail?email="+data['email']+"'>链接</a>验证")

    return jsonify({'code':200})


@bp_user.route('validEmail')
def validEmail():
    email = request.args.get('email')
    sql = "update user set status=1 where email='%s'"%(email)
    db1.update(sql)
    return redirect('http://localhost:8888')